using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SomeRandomGame.Interfaces;
using SomeRandomGame.Particles;

namespace SomeRandomGame.Guns
{
    public class LaserGun : IGun
    {
        public Vector2 EmitterLocation { get; set; }
        public double FireRate { get; set; }
        public List<IBullet> Bullets { get; set; }

        private readonly Texture2D _texture;
        private readonly SoundEffect _laserSound;
        private double _lastShot;
        private readonly Effect _effect;

        public LaserGun(Texture2D texture, Vector2 location, double fireRate, SoundEffect laserSound, Effect effect)
        {
            EmitterLocation = location;
            FireRate = fireRate;
            Bullets = new List<IBullet>();

            _texture = texture;
            _laserSound = laserSound;
            _effect = effect;
        }

        public void Initialize(IGameManager gameManager)
        {
        }

        public void Update(IGameManager gameManager)
        {
            for (int particle = 0; particle < Bullets.Count; particle++)
            {
                Bullets[particle].Update(gameManager);
                if (Bullets[particle].Position.X >= gameManager.Game.GraphicsDevice.Viewport.Width)
                {
                    Bullets.RemoveAt(particle);
                    particle--;
                }
            }

        }

        public void Draw(SpriteBatch spriteBatch, IGameManager gameManager, GameTime gameTime)
        {
            //_effect.Parameters["BlurDistance"].SetValue(0.1f);
            //spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, null, null, null, _effect);
            //foreach (LaserParticle laser in Bullets)
            //{
            //    laser.Draw(spriteBatch, gameManager, gameTime);
            //}
            //spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
            foreach (LaserParticle laser in Bullets)
            {
                laser.Draw(spriteBatch, gameManager, gameTime);
            }
            spriteBatch.End();
        }

        public void Shoot(IGameManager gameManager, IInputManager inputManager)
        {
            if (!inputManager.IsShooting()) return;
            double now = gameManager.GameTime.TotalGameTime.TotalMilliseconds;
            if (now - _lastShot > 1000/FireRate)
            {
                Bullets.Add(GetNewBullet());
                _laserSound.Play();
                _lastShot = now;
            }
        }

        private LaserParticle GetNewBullet()
        {
            Vector2 position = EmitterLocation;
            Vector2 velocity = new Vector2(5, 0);
            const float angle = 0;
            const float angularVelocity = 0;
            Color color = Color.DarkViolet;

            LaserParticle bullet = new LaserParticle(_texture, position, velocity, angle, angularVelocity, color);
            return bullet;
        }
    }
}
